package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)

type User struct {
	Id        int       `json:"id,omitempty"`
	Username  string    `json:"username,omitempty"`
	Email     string    `json:"email,omitempty"`
	CreatedAt time.Time `json:"created_at"`
}

func main() {
	dsn := "root:123456@tcp(localhost:3306)/article?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	u1 := User{
		Username: "user1",
		Email:    "user1@gmail.com",
	}
	err1 := db.Create(&u1).Error
	if err1 != nil {
		fmt.Println("创建错误：", err1.Error())

	}
	result1 := db.First(&u1)
	if result1.Error != nil {
		fmt.Println("failed find user1")
	}
	fmt.Println(result1)
}
